package com.zmn.oms.task.config;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 类描述：线程池配置
 * @author lujia
 * @date 2018/11/23 19:53
 */
@Configuration
public class ThreadPoolConfig {

    private static int CORE_POOL_SIZE = 5;
    private static int MAX_POOL_SIZE = 10;

    @Bean(autowire = Autowire.BY_NAME,value = "taskAsyncPool")
    public Executor taskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setThreadNamePrefix("TaskAsyncExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    /**
     * 同步师傅按单保险专用线程池，其他功能请勿使用！！！！
     * @return
     * @author lhl
     * @date 2020-06-17 18:44
     */
    @Bean(autowire = Autowire.BY_NAME,value = "insOrderSyncExecutor")
    public ThreadPoolTaskExecutor insOrderSyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(20);  // 队列长度
        executor.setKeepAliveSeconds(60 * 5); // 线程空闲时间: 5分钟
        executor.setThreadNamePrefix("InsOrderSyncExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        executor.initialize();
        return executor;
    }
}
